<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once ('PrunusPlugin.php');

/**
 * PrunusLib
 * 
 * The library.
 * Plugins container. 
 * Main entry point.
 * 
 * @package Prunus   
 * @author agosto <agosto@7dedos.com>
 * @copyright	Copyright (c) 2009-2010, 7dedos
 * @link http://www.7dedos.com
 * @version 0.3
 * @access public
 */
class PrunusLib extends PrunusLibPlugin {
  private $plugins;
  
  
	/**
	 * PrunusLib::__construct()
	 *
     * Initializes the class and loads every plugun 
     *   
	 * @return
	 */
	public function __construct()	{
    parent::__construct();
    $this->plugins = array();
    $this->version='0.3';
    $this->name='Prunus Core Library';
    $this->path = strtolower(@realpath(dirname(__FILE__)));
    $this->file = strtolower(@pathinfo(__FILE__, PATHINFO_BASENAME));

    $this->ci->load->config('PrunusLib');
    $this->ci->output->enable_profiler($this->ci->config->item('PRUNUSLIB_show_profiler'));  
    $this->ci->load->library('session');
    
    //se barre el directorio actual buscando archivos que empiecen con prunus (!= prunusplugin)
    $files = scandir(dirname(__FILE__));
    foreach ($files as $fileOriginal) {
      $file = strtolower($fileOriginal);
      if (substr($file,0,6) === 'prunus') {
        if (substr($file,0,12) != 'prunusplugin') {
          if ($file != strtolower(@pathinfo(__FILE__, PATHINFO_BASENAME))) {
            $this->ci->load->library($file);
            $className = substr($fileOriginal,0,-4);
            if (class_exists($className)) {
              $obj =& instantiate_class(new $className());
              if (is_a($obj,"PrunusLibPlugin")) $this->plugins[$className] = $obj;
            }
          }
        }
      }
    }
		log_message('debug', 'Prunus Library inicializada');
  }
  
	/**
	 * PrunusLib::__destruct()
	 * 
	 * @return
	 */
	public function __destruct ()	{
		log_message('debug', 'Prunus Library destruida');
  }
  
  /**
   * PrunusLib::whoami()
   * 
   * Calls whoami of everyplugin registered
   * 
   * @return string 
   */
  public function whoami() {
    $out = parent::whoami(). "<hr />";
    foreach ($this->plugins as $plugin) $out .= $plugin->whoami() . "<br />";
    $out .= "<hr />";
    return ($out);
  }
}
